import msg from "../../../utils/msg";
import { downLoadComTem, importPlayer } from "../../../utils/request/api";
import User from "../../../utils/user";

// pages/compete/import/import.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    res:{
      success:0 as number,
      fail:0 as number,
      msg:[
        
      ] as any,
    },
    loading:true,
    com:{
      game_id:0,
      game_data_id:0,
      game_title:'',
    },
    active:0,
    start:false,
  },
  list(){
    wx.navigateTo({
      url: "../player/player?com=" + this.data.com,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options:any) {
    if(!options.com){
      wx.navigateBack();
    }
    this.setData({
      com:JSON.parse(options.com),
    });
  },
  downLoad:async function(){
    msg.Loading();
    await downLoadComTem();
    msg.hideLoading();
  },
  import(){
    let self = this;
    let user = new User;
    wx.chooseMessageFile({
      count:1,
      type:'file',
      async success(res){
        msg.Loading();
        self.setData({
          active:1,
          start:true,
        });
        const tempFilePath = res.tempFiles[0].path;
        await importPlayer({filePath:tempFilePath,GF_ID:user.GF_ID,game_id:self.data.com.game_id,game_data_id:self.data.com.game_data_id})
        .then((res:any)=>{
          let data = JSON.parse(res.data);
          self.setData({
            res:data.data,
            loading:false,
            active:1,
          });
          msg.hideLoading();
        ;})
        .catch(async (err)=>{
          msg.hideLoading();
          console.log(err);
          await msg.show('导入失败，请检查文件格式');
          //wx.navigateBack();
        });
        msg.hideLoading();
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})